Ext.onReady(function() {
	Ext.ns("Heb.pwdMemo");
	
	basicAction = "webview/pwdMemo";
	
	/** 详情 **/
	var detailForm = Ext.create('Ext.form.Panel', {
		bodyPadding : 5,
		width : 400,
		height: 300,
		bodyPadding: 20,
		layout : 'anchor',
		buttonAlign : 'center',
		defaultType : 'textfield',
		defaults: {
			anchor: '100%',
			labelStyle : "text-align:right;",
			readOnly: true
		},
		items : [{//pwdMemoId
			fieldLabel : 'id',
			labelWidth: 80,
			name : 'pwdMemoInfo.pwdMemoId'
		}, {//时间
			fieldLabel : '创建时间',
			labelWidth: 80,
			name : 'pwdMemoInfo.createTime'
		}, {//类型
			fieldLabel : '类型',
			labelWidth: 80,
			name : 'pwdMemoInfo.typeName'
		}, {//账号
			fieldLabel : 'account',
			labelWidth: 80,
			name : 'pwdMemoInfo.accountNumber'
		}, {//密文
			xtype: 'textareafield',
			fieldLabel : '密文',
			labelWidth: 80,
			name : 'pwdMemoInfo.encryptPwd'
		}, {//备注
			xtype: 'textareafield',
			fieldLabel : '备注',
			labelWidth: 80,
			name : 'pwdMemoInfo.remark'
		}],
		buttons : [{
			text : '关闭',
			handler : function() {
				detailWin.close();
			}
		}]
	});
	
	/** memo详情 **/
	var detailWin = Ext.create('Ext.window.Window', {
		title: 'Memo详情',
		closeAction : 'hide',//关闭时隐藏
		modal: true,
		y: 100,
		resizable : false,
		//autoShow: true,
		items: detailForm
	});
	
	/** 查看详情 **/
	showMemoDetail = function(rowNum){
		var oneRecord = pwdStore.getAt(rowNum);
		
		var pwdMemoId = oneRecord.get("pwdMemoId");
		var createTime = oneRecord.get("createTime");
		var typeName = oneRecord.get("typeName");
		var accountNumber = oneRecord.get("accountNumber");
		var encryptPwd = oneRecord.get("encryptPwd");
		var remark = oneRecord.get("remark");
		
		var valuesJson = {};
		valuesJson['pwdMemoInfo.pwdMemoId'] = pwdMemoId;
		valuesJson['pwdMemoInfo.typeName'] = typeName;
		valuesJson['pwdMemoInfo.accountNumber'] = accountNumber;
		valuesJson['pwdMemoInfo.encryptPwd'] = encryptPwd;
		valuesJson['pwdMemoInfo.createTime'] = createTime;
		valuesJson['pwdMemoInfo.remark'] = remark;
		
		detailForm.getForm().reset();
		detailForm.getForm().setValues(valuesJson);
		
		detailWin.show();
	}
	
	/** ============================== 修改form ============================== **/
	Heb.pwdMemo.memoGridParameter = {};

	/** 领域 form 参数 **/
	Heb.pwdMemo.memoFormConfig = {
		url : basicAction + '_insertPwdMemo',
		success : function(form, action) {
			formSaveMask.hide();
			if(action.result.success == true){
				insertWin.close();
				
				reloadMemoGrid();
				
				Heb.alert('保存成功', action.result.msg, 30);
			}else{
				Heb.alert('保存失败', action.result.msg, 30);
			}
		},
		failure : function(form, action) {
			formSaveMask.hide();
			Heb.alert('操作失败', "保存失败", 30);
		}
	};
	
	/** memo form **/
	var memoForm = Ext.create('Ext.form.Panel', {
		bodyPadding : 5,
		width : 400,
		height: 260,
		bodyPadding: 20,
		layout : 'anchor',
		buttonAlign : 'center',
		defaultType : 'textfield',
		trackResetOnLoad: true,
		defaults: {
			anchor: '100%',
			labelStyle : "text-align:right;"
		},
		//pwdMemoId, typeName, accountNumber, encryptPwd, createTime, remark;
		items : [{//类型
			fieldLabel : '类型',
			labelWidth: 80,
			name : 'pwdMemoInfo.typeName',
			allowBlank : false,
			blankText : "请输入类型名称",
			regex: /^.{1,200}$/,
			regexText: "1-200 位任意字符"
		}, {//账号
			fieldLabel : 'account',
			labelWidth: 80,
			name : 'pwdMemoInfo.accountNumber',
			allowBlank : false,
			blankText : "请输入account",
			regex: /^.{1,200}$/,
			regexText: "1-200 位任意字符"
		}, {//密文
			xtype: 'textareafield',
			fieldLabel : '密文',
			labelWidth: 80,
			name : 'pwdMemoInfo.encryptPwd',
			allowBlank : false,
			blankText : '密文不能为空',
			regex: /^[\s\S]{1,500}$/,
			regexText: "1-500 位任意字符"
		}, {//备注
			xtype: 'textareafield',
			fieldLabel : '备注',
			labelWidth: 80,
			name : 'pwdMemoInfo.remark',
			allowBlank : true,
			regex: /^[\s\S]{0,500}$/,
			regexText: "0-500 位任意字符"
		}, {//pwdMemoId 隐藏域
			id: "pwdMemoId",
			hidden: true,
			name: "pwdMemoInfo.pwdMemoId"
		}],
		buttons : [{
			text : '取消',
			handler : function() {
				insertWin.close();
			}
		}, {
			text : '保存',
			formBind : true, //form 验证通过才可用
			disabled : true,
			handler : function() {
				formSaveMask.show();
				
				var form = this.up('form').getForm();
				if (form.isValid()) {
					form.submit(Heb.pwdMemo.memoFormConfig);
				}else{
					Heb.alert('警告', '输入有误！', 30);
				}
			}
		}]
	});
	
	/** 插入memo窗口 **/
	var insertWin = Ext.create('Ext.window.Window', {
		title: '新增Memo',
		closeAction : 'hide',//关闭时隐藏
		modal: true,
		y: 100,
		resizable : false,
		autoScroll: true,
		//autoShow: true,
		items: memoForm
	});
	
	
	/** 新增memo **/
	var insertNewMemo = function(){
		insertWin.setTitle("新增memo");
		
		/** 设置url **/
		Heb.pwdMemo.memoFormConfig.url = basicAction + '_insertPwdMemo';
		
		var valuesJson = {};
		valuesJson['pwdMemoInfo.pwdMemoId'] = '';
		valuesJson['pwdMemoInfo.typeName'] = '';
		valuesJson['pwdMemoInfo.accountNumber'] = '';
		valuesJson['pwdMemoInfo.encryptPwd'] = '';
		//valuesJson['pwdMemoInfo.createTime'] = '';
		valuesJson['pwdMemoInfo.remark'] = '';
		
		memoForm.getForm().setValues(valuesJson);
		memoForm.getForm().isValid();
		memoForm.getForm().reset();
		insertWin.show();
	}
	
	
	/** 修改 **/
	var editMemo = function(){
		var selectRecord = pwdGrid.getSelectionModel().getSelection();
		if(selectRecord.length == 0){
			Heb.alert('警告', "请选择要修改的行！", 30);
		}else if(selectRecord.length > 1){
			Heb.alert('警告', "只能选择一行！", 30);
		}else{
			var oneRecord = selectRecord[0];
			
			var pwdMemoId = oneRecord.get("pwdMemoId");
			var typeName = oneRecord.get("typeName");
			var accountNumber = oneRecord.get("accountNumber");
			var encryptPwd = oneRecord.get("encryptPwd");
			var remark = oneRecord.get("remark");
			
			var valuesJson = {};
			valuesJson['pwdMemoInfo.pwdMemoId'] = pwdMemoId;
			valuesJson['pwdMemoInfo.typeName'] = typeName;
			valuesJson['pwdMemoInfo.accountNumber'] = accountNumber;
			valuesJson['pwdMemoInfo.encryptPwd'] = encryptPwd;
			//valuesJson['pwdMemoInfo.createTime'] = createTime;
			valuesJson['pwdMemoInfo.remark'] = remark;
			
			insertWin.setTitle("修改Memo");
			
			/** 设置url **/
			Heb.pwdMemo.memoFormConfig.url = basicAction + '_editPwdMemo';
			
			memoForm.getForm().setValues(valuesJson);
			memoForm.getForm().reset();
			
			insertWin.show();
		}
	}
	
	/** delete memo **/
	var deleteMemo = function(){
		var selectRecord = pwdGrid.getSelectionModel().getSelection();
		if(selectRecord.length == 0){
			Heb.alert('警告', "请选择要删除的行！", 30);
		}else{
			Heb.confirm('警告','删除之后不可恢复，确定要删除吗？', 30, function(btn){
				if (btn == 'yes') {
					deleteMask.show();
					
					var pwdMemoIdArray = [];
					
					for(var i = 0;i<selectRecord.length;i++){
						var oneRecord = selectRecord[i];
						
						var pwdMemoId = oneRecord.get("pwdMemoId");
						
						pwdMemoIdArray.push(pwdMemoId);
					}
					
					var pwdMemoIdArrayStr = pwdMemoIdArray.join(",");
					
					var parameters = {};
					parameters["intIdArrayStr"] = pwdMemoIdArrayStr;
					
					Ext.Ajax.request({
						url: basicAction + "_deletePwdMemos",
						method: "post",
						async : true,
						params: parameters,
						success: function(response, config){
							var resultJson = Ext.JSON.decode(response.responseText);
							
							if(resultJson.success == true){
								reloadMemoGrid();
								
								Heb.alert('删除成功', resultJson.msg, 30);
							}else{
								Heb.alert('删除失败', resultJson.msg, 30);
							}
						},
						failure: function(){
							Ext.MessageBox.alert("失败", "请求失败");
						},
						callback: function(options, success, response){
							deleteMask.hide();
						}
					});
				}
			});
		}
	}
	
	/** 重新加载 **/
	var reloadMemoGrid = function(){
		pwdStore.load({params: Heb.pwdMemo.memoGridParameter});
	}
	
	/** ============================== grid ============================== **/
	
	/** 按钮工具栏 **/
	var pwdTopButton = [{
		xtype: 'button',
		text: '新增',
		icon: Heb.icon.buildAdd,
		handler : function() {
			insertNewMemo();
		}
	},{
		xtype: 'button',
		text: '修改',
		icon: Heb.icon.buildEdit,
		handler : function() {
			editMemo();
		}
	},{
		xtype: 'button',
		text: '删除',
		icon: Heb.icon.buildDel,
		handler : function() {
			deleteMemo();
		}
	}];
	
	/** ========== store 需要使用的 model ========== **/
	Ext.define('pwdModel', {
		extend : 'Ext.data.Model',
		fields : [
			{name : 'pwdMemoId', mapping : 'pwdMemoId', type : 'int'},
			{name : 'userId', mapping : 'userId', type : 'int'},
			{name : 'typeName', mapping : 'typeName', type : 'string'},
			{name : 'accountNumber', mapping : 'accountNumber', type : 'string'},
			{name : 'encryptPwd', mapping : 'encryptPwd', type : 'string'},
			{name : 'createTime', mapping : 'createTime',type : 'string'},
			{name : 'remark', mapping : 'remark', type : 'string'}
		]
	});
	
	/** ========== store ========== **/
	var pwdStore = Ext.create('Ext.data.Store', {
		model : 'pwdModel',
		autoLoad: true,
		pageSize : 50,
		proxy: {
			//simpleSortMode: true,
			type: 'ajax',
			url: basicAction + '_getPwdMemoList',
			reader: {
				type: 'json',
				root: 'items',
				totalProperty : 'total'
			}
		},
		listeners:{
			load: function(){
				//设置基本参数，防止刷新翻页时，参数丢失
				Ext.apply(pwdStore.proxy.extraParams, Heb.pwdMemo.memoGridParameter);
			}
		}
	});
	
	
	/** pwd列 **/
	var pwdColumn = [
		{xtype: 'rownumberer'},
		{header: 'id', dataIndex: 'pwdMemoId', align: 'left', width: 70},
		//{header: '用户Id', dataIndex: 'userId', align: 'left', width: 150},
		{header: '类型', dataIndex: 'typeName', align: 'left', width: 150},
		{header: 'account', dataIndex: 'accountNumber', align: 'left', width: 150},
		{header: '密文', dataIndex: 'encryptPwd', align: 'left', width: 200},
		{header: '创建时间', dataIndex: 'createTime', align: 'left', width: 150},
		{header: '备注', dataIndex: 'remark', align: 'left', width: 150},
		{header: '操作', dataIndex: 'pwdMemoId', align: 'center', width: 100, renderer : function(value, cellmeta, record, row){
			var buttonHtml = "<span class='extSpButton'>";
			buttonHtml = buttonHtml + "<a href='javascript:void(0)' onclick='showMemoDetail(\"" + row + "\")'>查看详情</a></span>";
			return buttonHtml;
		}}
	];
	
	/** 分页工具栏 **/
	var pageBar = Ext.create('Ext.toolbar.Paging', {
		store: pwdStore,
		displayInfo: true,
		displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
		emptyMsg: "没有数据"
	});
	
	var selModel = Ext.create('Ext.selection.CheckboxModel');
	
	/** ========== 功能配置表格 ========== **/
	var pwdGrid = Ext.create('Ext.grid.Panel', {
		title : '密码备忘',
		border : true,
		//height: 1000,
		//autoScroll : true,
		width : 1024,
		store : pwdStore,
		tbar : pwdTopButton,
		bbar : pageBar,  //分页工具栏
		columns : pwdColumn,
		selModel: selModel,//多选框
		stripeRows: true,  //斑马线
		renderTo: 'pwdMemoGridDiv',
		viewConfig : {
			loadMask: true
		}
		//renderTo: Ext.getBody()
	});
	
	
	var deleteMask = new Ext.LoadMask(Ext.getBody(), {msg: "正在删除..."});
	var formSaveMask = new Ext.LoadMask(insertWin, {msg: "正在保存..."});
});